// pages/transfer/transferAbnormal/transferAbnormal.js
import { listUnusuals } from "../../../api/tansfer";
import dayjs from "dayjs";

const statusLabelMap = {
  1: '待审核',
  2: '已驳回',
  3: '已通过',
};
const statusStyleMap = {
  1: "color: #8E8E8E",
  2: "color: #4A90E2",
  3: "color: green",
};

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    statusLabelMap,
    statusStyleMap,
    timeStart: dayjs().format('YYYY-MM-DD 00:00:00'),
    timeEnd: dayjs().add(1, 'day').format('YYYY-MM-DD 00:00:00'),
    dataList: [],
    pageNo: 1,
    totalPages: 1,
    showFoot: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    loadData() {
      return this.getListUnusuals()
    },
    getListUnusuals() {
      const {pageNo, dataList, timeStart, timeEnd} = this.data;
      this.isLoading = true;
      return listUnusuals({
        timeStart: timeStart,
        timeEnd: timeEnd,
        page: pageNo,
        size: 10,
      })
        .then(res => {
          const result = (res.data && res.data.result) || {}
          let tempArr = [];
          let showf;
          if (pageNo == 1) {
            tempArr = result.content || [];
            showf = 0;
          } else {
            this.data.totalPages = result.totalPages
            tempArr = dataList.concat(result.content);
            showf = (this.data.pageNo > result.totalPages) ? 2 : 0;
          }
          this.setData({
            //复制数据源
            dataList: tempArr,
            showFoot: showf,
          });
        })
    },
    handledDateRange(event) {
      const {start, end} = event.detail
      this.setData({
        timeStart: start,
        timeEnd: end
      })
      console.log(start, end)
    },

    refresh() {
      this.data.pageNo = 1
      this.loadData()
    },

    // 复制
    clipboardData(e) {
      wx.setClipboardData({
        data: e.currentTarget.dataset.text,
      })
    },

    // 页面相关事件处理函数
    onPullDownRefresh() {
      this.data.pageNo = 1;
      this.loadData().then(res => {
        wx.stopPullDownRefresh()
      })
    },

    // 页面上拉触底事件的处理函数
    onReachBottom() {
      //如果是正在加载中或到底了，则返回
      if (this.data.showFoot != 0) {
        return;
      }
      //如果当前页大于或等于总页数，那就是到最后一页了，返回
      if (this.data.pageNo > this.data.totalPages || this.data.totalPages == 0) {
        return;
      } else {
        this.data.pageNo++;
      }
      //底部显示正在加载更多数据
      this.setData({ showFoot: 1 });
      //获取数据
      this.loadData();
    },
  },

  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      const {timeStart, timeEnd} = this.data
      const dateRangeCom = this.selectComponent('#dateRange')
      dateRangeCom.setDefaultDate([timeStart, timeEnd])
      this.loadData()
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
})
